$(function () {
  //需求1:渲染用户信息,
  let layer = layui.layer;
  let form = layui.form;
  initUserInfo();
  function initUserInfo() {
    //获取用户信息
    axios({
      method: "GET",
      url: "/my/userinfo",
      //   headers: {
      //     Authorization: localStorage.getItem("token"),
      //   },
    }).then((res) => {
      //console.log(res.data);
      //判断
      let obj = res.data;
      if (obj.status !== 0) {
        return layer.msg(obj.message);
      }
      //赋值
      form.val("formUserInfo", obj.data);
    });
  }
  //需求2:自定义校验
  form.verify({
    nickname: function (value) {
      if (value.length > 10) {
        return "用户昵称在1-10位之间";
      }
    },
  });

  //需求3:修改用户信息
  $("form").on("submit", (e) => {
    e.preventDefault();
    axios({
      method: "POST",
      url: "/my/userinfo",
      data: $("form").serialize(),
      //   headers: {
      //     Authorization: localStorage.getItem("token"),
      //   },
    }).then((res) => {
      // console.log(res.data);
      let obj = res.data;
      if (obj.status !== 0) {
        return layer.msg(obj.message);
      }
      layer.msg(obj.message);
      window.parent.getUserInfo();
    });
  });

  //需求4: 重置 按钮的点击事件和form的重置事件
  $("form").on("reset", function (e) {
    e.preventDefault();
    initUserInfo();
  });
});
